package eg.com.itiga.repositoryimpl.itian;

import eg.com.itiga.domain.Department;
import eg.com.itiga.domain.Itian;
import eg.com.itiga.domain.ItianCompany;
import eg.com.itiga.repository.base.BaseDao;
import eg.com.itiga.repository.exception.DataException;
import eg.com.itiga.repository.factory.RepositoryFactory;
import eg.com.itiga.repository.itian.ItianDao;
import eg.com.itiga.repositoryimpl.base.BaseDaoImpl;
import eg.com.itiga.repositoryimpl.base.HibernateUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.*;
import org.hibernate.criterion.*;

/**
 * 
 * @author Tarek
 */

public class ItianDaoImpl extends BaseDaoImpl implements ItianDao {
	
	public Itian retriveITIanData(int id) throws DataException
    {
       
        Itian i = new Itian();
       
        Session session=HibernateUtil.getCurrentSession();
        Criterion userIDeq=Restrictions.eq("user.userId", id);
        Criteria crt = session.createCriteria(Itian.class);
        crt.add(userIDeq);
        //session.load(c, id);
        i=(Itian)crt.uniqueResult();      
       
        return i;
    }

	public List<Itian> searchItian(String name, int department, int intake)
			throws DataException {
		// TODO Auto-generated method stub

		Session session = HibernateUtil.getCurrentSession();
		Criteria cri = session.createCriteria(Itian.class);
		//if (!(name.equals("")) && name != null) {
			System.out.println("Name Criteria"); 
			Criterion nameCriterion = Restrictions.eq("IName", name);
			cri.add(nameCriterion);
		//}
		//if (department != 0) {
			System.out.println("Department Criteria");
			Criterion criterion = Restrictions.eq("IId", department);
			cri.add(criterion);
		//}
		
			
		//if (intake != 0) {
			System.out.println("Intake Criteria");
			Criterion intakeCriterion = Restrictions.eq("IIntake", intake);
			cri.add(intakeCriterion);
		//}
		List<Itian> result = cri.list();

		return result;
	}

	public List<Department> getAllDepartment() throws DataException {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getCurrentSession();

		Criteria criteria = session.createCriteria(Department.class);
		List<Department> list = criteria.list();
		session.close();
		return list;
	}

	public Itian getUserData(int uid) throws DataException {
		// TODO Auto-generated method stub
		List<Itian> itianList = null;
		Itian itian = null;//new Itian();
		Session session = HibernateUtil.getCurrentSession();
		
		//user = userDao.getUserByCredential(user);	
		Criteria cri = session.createCriteria(Itian.class);
		itianList = cri.list();
		for(int i=0;i<itianList.size();i++)
		{
			if(itianList.get(i).getUser().getUserId() == uid)
			{
				itian = itianList.get(i); 
				System.out.println("User ID: "+itianList.get(i).getUser().getUserId());
				System.out.println("User Name: "+itianList.get(i).getIName());
			}
		}
		return itian;
	}

	@Override
	public List<Itian> getUnWorkedItians(List<Itian> selectedList) {
		// TODO Auto-generated method stub
		List<Itian> resultList=new ArrayList<Itian>();
		for(Itian itian : selectedList)
		{
			Set companiesSet=itian.getItianCompanies();
			boolean isWorked=false;
			for(Object obj : companiesSet)
			{
				ItianCompany c=(ItianCompany)obj;
				if(c.getIsCurrent())
					{
					isWorked=true;
					break;
					}
				
			}
			if(!isWorked)
				resultList.add(itian);
		}
		return resultList;
	}

	public List<Itian> getWorkedItians() throws DataException {
		
		List<Itian> itiansList = new ArrayList<Itian>();
		
		List<Itian> tempList = findAll(Itian.class);
		
		for (Iterator iterator = tempList.iterator(); iterator.hasNext();) {
			Itian itian = (Itian) iterator.next();
			
			Object[] companies = itian.getItianCompanies().toArray();
			
			for (int i = 0; i < companies.length; i++) {
				ItianCompany itianCompany = (ItianCompany) companies[i];
				
				if(itianCompany.getIsCurrent())
				{
					itiansList.add(itian);
				}
			}
		}
		
		return itiansList;
	}
	
	// TODO to be reviewed...
	public List<Itian> getItianByDeptAndIntake(int department, int intake)
			throws DataException {
		BaseDao base = RepositoryFactory.createBaseDao();
		beginTransaction();
		Department dept = (Department) base.find(Department.class, department);
		System.out.println(dept);
		System.out.println("=============what============");
		Set itianSet = dept.getItians();

		System.out.println("=============yes============");
		List<Itian> itianList = new ArrayList<Itian>();
		System.out.println(itianSet);
		for (Object obj : itianSet) {
			Itian i = (Itian) obj;
			System.out.println("=============what============");
			if (i.getIIntake().intValue() == intake) {
				System.out.println("=============what============");
				itianList.add(i);
			}
		}
		return itianList;
	}

}
